<?php namespace system\model;
use houdunwang\model\Model;
class Article extends Model{
	//数据表
	protected $table = "article";
	//允许填充字段
	protected $allowFill = [ ];

	//禁止填充字段
	protected $denyFill = [ ];

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
        ['title','isnull','文章标题不能为空',self::MUST_VALIDATE,self::MODEL_BOTH],
        ['author','isnull','文章作者不能为空',self::MUST_VALIDATE,self::MODEL_BOTH],
        ['thumb','isnull','文章缩略图不能为空',self::MUST_VALIDATE,self::MODEL_BOTH],
        ['digest','isnull','文章摘要不能为空',self::MUST_VALIDATE,self::MODEL_BOTH],           ['keywords','isnull','文章关键字不能为空',self::MUST_VALIDATE,self::MODEL_BOTH],
	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
        ['sendtime','time','function',self::MUST_AUTO,self::MODEL_INSERT],
        ['user_uid','getUid','method',self::MUST_AUTO,self::MODEL_BOTH],
        ['updatetime','time','function',self::MUST_AUTO,self::MODEL_BOTH]
        // title,author,category_cid,thumb,sendtime,digest,keywords,tag_tid
	];

    /**
     * 自动完成里的验证uid
     * @return mixed
     */
	public function  getUid(){
	    return Session::get('uid');
    }

    /**
     * 添加方法
     */
    public function addData(){
         if(!isset($_POST['tid'])){
             return ['valid'=>0,message=>'请选择标签'];
         }
        //1.添加文章表【两部分：下面第一部分+自动完成】
        //title、sendtime、thumb、digest、author、
        //keywords、user_uid、category_cid
        $this->title=$_POST['title'];
        $this->author=$_POST['author'];
        $this->thumb=$_POST['thumb'];
        $this->digest=$_POST['digest'];
        $this->keywords=$_POST['keywords'];
        $this->category_cid=$_POST['category_cid'];
        $aid=$this->save();
        //添加文章表
        // des content article_aid
         $articleDataModel = new \system\model\ArticleData();
         $articleDataModel->des=$_POST['des'];
         $articleDataModel->content=$_POST['content'];
         $articleDataModel->article_aid=$aid;
         $articleDataModel->save();
         //添加文章-标签关系表 article_aid tag_tid

        $articleTagModel= new \system\model\ArticleTag();
           foreach ($_POST['tid'] as $v){
               $articleTagModel->article_aid=$aid;
               $articleTagModel->tag_tid=$v;
               $articleTagModel->save();
           }
           return ['valid'=>1,'message'=>'操作成功'];
    }
    /**
     * 获取分类数据
     */
    public function getCateData(){
       return Arr::tree(Db::table('category')->get(),'cname');
    }
    /**
     * 获取标签
     * @return mixed
     */
    public function getTagData(){
        return Db::table('tag')->get();
    }

    /**
     * 获得所有数据
     */
    public function getAllData($num,$status){
        //获取数据总条数
      $count=Db::table('article')->where('is_recycle',$status)->count();
        Page::row($num)->make($count);
       $page=Page::desc(['pre'=>'上楼', 'next'=>'下楼','first'=>'首页','end'=>'尾页','unit'=>'个'])->make($count,$num);
      $data=Db::table('article')
          ->join('category','article.category_cid','=','category.cid')
          ->where('is_recycle',$status)->limit(Page::limit())->get();
      return ['page'=>$page,'data'=>$data];
    }

    /**
     * 获得旧数据
     */
    public function  getOldData($aid){
       return Db::table('article')
           ->join('article_data','article_data.article_aid','=','article.aid')
           ->where('article.is_recycle',0)->where('aid',$aid)->first();
    }

    /**
     * 编辑文章
     * @param $aid 要编辑的文章id号码
     */
    public function editArticle(){
        if(!empty($_POST['tag'])){
           return ['valid'=>0,'message'=>'操作失败'];
        }
         //1.编辑article表
        //1.1获取模型对象
        $aid=$_POST['aid'];
        $articleModel=$this->find($aid);
        //1.2更新字段
        $articleModel->title=$_POST['title'];
        $articleModel->author=$_POST['author'];
        $articleModel->thumb=$_POST['thumb'];
        $articleModel->digest=$_POST['digest'];
        $articleModel->keywords=$_POST['keywords'];
        $articleModel->category_cid=$_POST['category_cid'];
        //1.3更新
        $articleModel->save();
        //2 编辑articleData表
        //2.1 获取模型对象
        $ad_id=Db::table('article_data')->where('article_aid',$aid)->pluck('ad_id');
        $articleDataModel=ArticleData::find($ad_id);
        $articleDataModel->des=$_POST['des'];
        $articleDataModel->content=$_POST['content'];
        $articleDataModel->article_aid=$aid;
        $articleDataModel->save();
       //3.编辑articleTag关系表
        $articleTagModel = new Articletag();
        //3.1删除旧数据
        $articleTagModel->where('article_aid',$aid)->delete();
        //3.2新增
        foreach($_POST['tid'] as $k=>$v){
            $articleTagModel->article_aid=$aid;
            $articleTagModel->tag_tid=$v;
            $articleTagModel->save();
        }
        return ['valid'=>1,'message'=>'操作成功'];

    }
   /**
    * 删除文章
    */
   public function realDel($aid){
       //删除article表中的数据
        $this->where('aid',$aid)->delete();
       (new ArticleData())->where('article_aid',$aid)->delete();
       (new Articletag())->where('article_aid',$aid)->delete();
       return true;
   }

}
